package com.bcxk.payServer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bcxk.common.Respond;
import com.bcxk.entity.dto.Order;
import com.bcxk.entity.dto.Room;
import com.bcxk.payServer.wxPaySdk.MyWXConfig;
import com.bcxk.payServer.wxPaySdk.WXPay;
import com.bcxk.service.OrderService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author NieMingkun
 * 微信支付
 */
@Controller
@RequestMapping("wechat")
public class WechatController {

@Autowired
OrderService orderService;
    @PostMapping("getQRcode")
    @ResponseBody
    public Respond getQRcode(@RequestParam(value = "id" , required = false) long id ){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Order orders = orderService.getById(id);
        Room room =new Room();
        WXPay wxPay = null;
        try {
            wxPay = new WXPay(new MyWXConfig());
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "123457");  //商品描述
        data.put("out_trade_no", "1234657"); //外部订单号
        data.put("fee_type", "CNY"); //币种
        data.put("total_fee", "1"); //金额，单位:分 ! 不要改!!
        data.put("spbill_create_ip", "127.0.0.1"); //终端ip
        data.put("notify_url", "http://szrip7.natappfree.cc/orders/updateStatus"); //回调地址
        data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
        data.put("product_id","1111111"); //商品id
        data.put("attach", "8888888888"); //内部订单号
        Map<String, String> resp = null;
        try {
            resp = wxPay.unifiedOrder(data);
            String resultCode = resp.get("result_code");
            //  if ("FAIL".equals(resultCode)) {
            // return Result.error(resp.get("err_code_des"));
            return Respond.ok(resp.get("code_url"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Respond.ok(resp.get("code_url"));
    }

}
